package com.crossdomain.www.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crossdomain.common.ControllerResponseBody;
import com.crossdomain.www.controller.domain.User;
import com.crossdomain.www.controller.domain.UserMessage;

@Controller
@RequestMapping("/user/")
public class TestJsonPController {
    @RequestMapping("/findUserMessageNoLoginJsonp.action")
    public @ResponseBody ControllerResponseBody<UserMessage> findUserMessageNoLogin(@RequestBody String json) {
        ControllerResponseBody<UserMessage> controllerResponseBody = new ControllerResponseBody<UserMessage>();
        UserMessage userMessage = new UserMessage();
        userMessage.setContent("消息内容");
        userMessage.setId(1231213L);
        userMessage.setReadStatus(true);
        userMessage.setTitle("消息标题");
        userMessage.setUserId(1L);
        controllerResponseBody.setData(userMessage);
        return controllerResponseBody;
    }

    @RequestMapping("/findUserMessageNeedLoginJsonp.action")
    public @ResponseBody ControllerResponseBody<UserMessage> findUserMessageNeedLogin(@RequestBody String json) {
        ControllerResponseBody<UserMessage> controllerResponseBody = new ControllerResponseBody<UserMessage>();
        UserMessage userMessage = new UserMessage();
        userMessage.setContent("消息内容");
        userMessage.setId(1231213L);
        userMessage.setReadStatus(true);
        userMessage.setTitle("消息标题");
        userMessage.setUserId(1L);
        controllerResponseBody.setData(userMessage);
        return controllerResponseBody;
    }

    @RequestMapping("/mockLoginJsonp.action")
    public @ResponseBody ControllerResponseBody<String> mockLogin(HttpSession session) {
        User user = new User();
        user.setId("1");
        user.setName("jannal");
        user.setPassword("afsfasdfasdf");
        session.setAttribute("user", user);
        ControllerResponseBody<String> controllerResponseBody = new ControllerResponseBody<String>();
        controllerResponseBody.setData("登录成功");
        return controllerResponseBody;
    }

    /**
var test = {};
test.text=function(){
        var _this = {
                init:function(){
                    $('#testId').text("this is remote js obj");
                }
        }
        return _this;
    }();
test.text.init();
     * @author jannal
     * @param session
     * @return
     */
    @RequestMapping("/mockLoginJsonp2.action")
    public @ResponseBody String mockLogin2(HttpSession session) {
        StringBuilder sb =new StringBuilder(); 
        sb.append("var test = {};");
        sb.append("test.text=function(){");
                        sb.append("var _this = {");
                            sb.append( "init:function(){");
                            sb.append("$('#testId').text('this is remote js obj');");
                            sb.append("}");
                       sb.append("};");
                       sb.append("return _this;");
                       sb.append( "}();");
                       sb.append("$(function() {test.text.init();});");
        return sb.toString();
    }
}
